package com.jifen.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

@WebFilter(filterName = "ReqLogFilter", urlPatterns = "/*")
public class ReqLogFilter implements Filter {

    private Logger logger = LoggerFactory.getLogger(ReqLogFilter.class);
    private List<String> patterns = new ArrayList();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        patterns.add("/h5/");
        patterns.add("/bootsrc/");
        patterns.add("/css/");
        patterns.add("/js/");
        patterns.add("/img/");
        patterns.add("/favicon.ico");
    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        if (!request.getRequestURI().contains(patterns.get(0)) && !request.getRequestURI().contains(patterns.get(1))
                && !request.getRequestURI().contains(patterns.get(2)) && !request.getRequestURI().contains(patterns.get(3))
                && !request.getRequestURI().contains(patterns.get(4)) && !request.getRequestURI().contains(patterns.get(5))) {
            if (!request.getRequestURI().equals("/")) {
                String userAgent = request.getHeader("user-agent");
                String sysType = "浏览器";
                if (userAgent != null && userAgent.indexOf("iPhone") > 0) {
                    sysType = "iPhone";
                } else if (userAgent != null && userAgent.indexOf("Android") > 0) {
                    sysType = "android";
                }
                if (request.getMethod().equals("GET")) {
                    logger.info("请求方式：{}   接口：{}   参数：{}   客户端：{}", "GET", request.getRequestURI(), request.getQueryString(), sysType);
                } else {
                    //获取表单中所有文本域的name
                    Enumeration<String> names = request.getParameterNames();
                    StringBuffer sb = new StringBuffer();
                    //循环获取枚举中所有的内容
                    int i = 0;
                    while (names.hasMoreElements()) {
                        //获取每一个文本域的name
                        String name = names.nextElement();
                        //根据name获得参数的值
                        //为了保证获取到所有的值  因为表单可能是单值  也可能是多值
                        String[] values = request.getParameterValues(name);
                        //输出参数名和参数值
                        if (i > 0) {
                            sb.append("&" + name + "=");
                        } else {
                            sb.append(name + "=");
                        }
                        for (String val : values) {
                            sb.append(val);
                        }
                        i++;
                    }
                    logger.info("请求方式：{}  接口：{}   参数：{}   客户端：{}", "POST", request.getRequestURI(), sb.toString(), sysType);
                }
            }
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }
}
